

cap file close Table
pause on 

local outcomes			$outcomes

local all_outcomes      $`outcomes'

local title				`outcomes'_endline_1_Tunisia


* Estimate ITTs using Tunisia endline 1 data 
				
	local count_out = 0 
	mat def pvalue = J(25,4,.)


	foreach outcome in `all_outcomes' {

	use  "Data/endline_1_tunisia.dta", clear 
	
		keep if female==0

		  local count_out = `count_out' + 1
			
			* Total ITT
			
				eststo total_itt: regress `outcome' total_itt i.Strata, vce (cluster imada)
				
					local c_1_`outcome' : di%12.3f _b[total_itt]
					local se_1_`outcome' : di%12.3f _se[total_itt]
					local n_1_`outcome' = e(N)
					local r2_1_`outcome' = e(R2)
					
					local p_1_`outcome' : di%12.3f ttail(e(df_r),abs(_b[total_itt]/_se[total_itt]))*2
					
					mat def pvalue[`count_out',1] = ttail(e(df_r),abs(_b[total_itt]/_se[total_itt]))*2
					
					sum `outcome' if total_itt ==0	
					local m_1_`outcome' : di%12.3f `r(mean)'

					local c_m1_`outcome'  =  `c_1_`outcome''/`m_1_`outcome''
					local c_m1_`outcome' 	 : di%12.3f `c_m1_`outcome'' 
		
			* Within
				
				eststo within: regress `outcome' within i.Strata, vce (cluster imada)
					
					local c_2_`outcome' : di%12.3f _b[within]
					local se_2_`outcome' : di%12.3f _se[within]
					local n_2_`outcome' = e(N)
					local r2_2_`outcome' = e(R2)
					local p_2_`outcome' : di%12.3f ttail(e(df_r),abs(_b[within]/_se[within]))*2
					
					mat def pvalue[`count_out',2] = ttail(e(df_r),abs(_b[within]/_se[within]))*2	
					
					sum `outcome' if within ==0	
					local m_2_`outcome' : di%12.3f `r(mean)'

					local c_m2_`outcome'  =  `c_2_`outcome''/`m_2_`outcome''
					local c_m2_`outcome' 	 : di%12.3f `c_m2_`outcome'' 
			
			* Between
			
				eststo between: regress `outcome' between i.Strata, vce (cluster imada)
				
					local c_3_`outcome' : di%12.3f  _b[between]
					local se_3_`outcome' : di%12.3f _se[between]
					local n_3_`outcome'  = e(N)
					local r2_3_`outcome' = e(R2)
					local p_3_`outcome' : di%12.3f ttail(e(df_r),abs(_b[between]/_se[between]))*2
					
					mat def pvalue[`count_out',3] = ttail(e(df_r),abs(_b[between]/_se[between]))*2	
					
					sum `outcome' if between ==0	
					local m_3_`outcome' : di%12.3f `r(mean)'

					local c_m3_`outcome'  =  `c_3_`outcome''/`m_3_`outcome''
					local c_m3_`outcome' 	 : di%12.3f `c_m3_`outcome'' 

					
			* Spillover  
			
				eststo spill: regress `outcome' spillovers i.Strata, vce (cluster imada)
				
					local c_4_`outcome' : di%12.3f _b[spillovers]
					local se_4_`outcome' : di%12.3f _se[spillovers]
					local n_4_`outcome' = e(N)
					local r2_4_`outcome' = e(R2)
					local p_4_`outcome' : di%12.3f ttail(e(df_r),abs(_b[spillovers]/_se[spillovers]))*2
					
					mat def pvalue[`count_out',4] = ttail(e(df_r),abs(_b[spillovers]/_se[spillovers]))*2
					
					sum `outcome' if spillovers ==0	
					local m_4_`outcome' : di%12.3f `r(mean)'

					local c_m4_`outcome'  =  `c_4_`outcome''/`m_4_`outcome''
					local c_m4_`outcome' 	 : di%12.3f `c_m4_`outcome'' 

			* GE  
			
				eststo infrastructure: regress `outcome' infrastructure i.Strata, vce (cluster imada)
				
					local c_9_`outcome' : di%12.3f _b[infrastructure]
					local se_9_`outcome' : di%12.3f _se[infrastructure]
					local n_9_`outcome' = e(N)
					local r2_9_`outcome' = e(R2)
					local p_9_`outcome' : di%12.3f ttail(e(df_r),abs(_b[infrastructure]/_se[infrastructure]))*2
					
					mat def pvalue[`count_out',4] = ttail(e(df_r),abs(_b[infrastructure]/_se[infrastructure]))*2
					
					sum `outcome' if infrastructure ==0	
					local m_9_`outcome' : di%12.3f `r(mean)'

					local c_m9_`outcome'  =  `c_9_`outcome''/`m_9_`outcome''
					local c_m9_`outcome' 	 : di%12.3f `c_m9_`outcome'' 
					
	}
				
				
			
* Store significance level based on p-value
	
	foreach outcome of local all_outcomes {
		
		local s_1_`outcome' ""
		local s_2_`outcome' ""
		local s_3_`outcome' ""
		local s_4_`outcome' ""
		local s_9_`outcome' ""
		
		* Total ITT 
		
			if `p_1_`outcome'' < 0.1{
				local s_1_`outcome' "*"
			}
			if `p_1_`outcome'' < 0.05{
				local s_1_`outcome' "**"
			}
			if `p_1_`outcome'' < 0.01{
				local s_1_`outcome' "***"
			}
			
		* Within
		
			if `p_2_`outcome'' < 0.1{
				local s_2_`outcome' "*"
			}
			if `p_2_`outcome'' < 0.05{
				local s_2_`outcome' "**"
			}
			if `p_2_`outcome'' < 0.01{
				local s_2_`outcome' "***"
			}
			
		* Between 
		
			if `p_3_`outcome'' < 0.1{
				local s_3_`outcome' "*"
			}
			if `p_3_`outcome'' < 0.05{
				local s_3_`outcome' "**"
			}
			if `p_3_`outcome'' < 0.01{
				local s_3_`outcome' "***"
			}
		
		* Spillover 
		
			if `p_4_`outcome'' < 0.1{
				local s_4_`outcome' "*"
			}
			if `p_4_`outcome'' < 0.05{
				local s_4_`outcome' "**"
			}
			if `p_4_`outcome'' < 0.01{
				local s_4_`outcome' "***"
			}
	
	    * GE 
		
			if `p_9_`outcome'' < 0.1{
				local s_9_`outcome' "*"
			}
			if `p_9_`outcome'' < 0.05{
				local s_9_`outcome' "**"
			}
			if `p_9_`outcome'' < 0.01{
				local s_9_`outcome' "***"
			}
	
	}
	
		
	foreach outcome of local all_outcomes {
		
		 local l_`outcome' : variable label `outcome'
		
	}
	

	
	
* Save tables to Dropbox



	file open Table using "Output/`title'.tex", text write replace

	file write Table  _n ///
	"\begin{tabular}{l*{13}{c}} \hline & \multicolumn{4}{c}{\textbf{Between}} & \multicolumn{4}{c}{\textbf{Within}} & \multicolumn{4}{c}{\textbf{Spillovers}} \\ \cmidrule(r){2-5}\cmidrule(l){6-9} \cmidrule(l){10-13} & $\beta$ & {SE} & {N} & \shortstack{Control\\mean} & $\beta$ & {SE} & {N} & \shortstack{Control\\mean}  & $\beta$ & {SE}  & {N} & \shortstack{Control\\mean} \\ \cmidrule(r){2-5}\cmidrule(l){6-9} \cmidrule(l){10-13} & {(1)} & {(2)} & {(3)} & {(4)} & {(5)} & {(6)} & {(7)} & {(8)} & {(9)} & {(10)} & {(11)} & {(12)} \\ \midrule \multicolumn{1}{p{1.3cm}}{\textbf{Tunisia}} & &  &    \\ \cmidrule(r){1-5} \cmidrule(l){6-9} \cmidrule(l){10-13}"   _n

	foreach outcome of local all_outcomes {
		
	file write Table																																	_n ///
	"`l_`outcome'' & `c_3_`outcome''`s_3_`outcome'' & `se_3_`outcome''   & `n_3_`outcome'' & `m_3_`outcome''   &  `c_2_`outcome''`s_2_`outcome'' & `se_2_`outcome'' & `n_2_`outcome'' & `m_2_`outcome'' & `c_4_`outcome''`s_4_`outcome'' & `se_4_`outcome''  & `n_4_`outcome'' &      `m_4_`outcome''                                                                                        \\"  _n 
	}


	file write Table					_n ///
			"\hline \end{tabular}"													_n

	file close Table

